--[[
FilePath: \Lua\View\BattleUI\UIChapterRecord.lua
主线过关推荐
--]]

require 'View/UIBase/UIBase'

UIChapterRecord = UIBase.New(PanelNames.UIChapterRecord)
local _M = UIChapterRecord

_M.hasInit = false
_M.cancelJumpOut = false

function _M.Init(cancelJumpOut, fightType, onPlayBegin, onPlayFinished, _replay_type, _foramtion_type)
    _M.cancelJumpOut = cancelJumpOut
    _M.fightType = fightType or EnumConst.FightTypeConst.DUNGEON
    _M.replay_type = _replay_type
    _M.foramtion_type = _foramtion_type
    _M.onPlayBegin = onPlayBegin
    _M.onPlayFinished = onPlayFinished
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self.title = self:GetChild("title"):GetChild("title")
        self.curStage = self:GetChild("curstage")
        self.dynamicList = self:GetChild("list")
        self.btn_back = self:GetChild("btn_back");
        self.bgClose = self:GetChild("bgClose");
        self.nothingController = self:GetController("nothing");
        self.jumpOut = self:GetTransition("JumpOut");
        self:addEvent()
        _M.hasInit = true
    end
     if not _M.cancelJumpOut then
        self.jumpOut:Play()
    end
    self:initUI();
    self:refreshWithData(nil);
    if self.fightType == EnumConst.FightTypeConst.DUNGEON then
        local nextId = logicMgr.MapManager.getNextStage();
        dungeonHandler.sendReqDungeonPassHistories(nextId,1);
    elseif self.fightType == EnumConst.FightTypeConst.StoryBattle then
        local nextId = 0
        if dungeonHandler.hardData.maxPassDungeon == 0 then
            nextId = 101
        else
            nextId = G.dataTable["t_stagehardmode"][tostring(dungeonHandler.hardData.maxPassDungeon)].f_NextStage
        end
        dungeonHandler.sendReqDungeonPassHistories(nextId,2);
    end
end

function _M:initUI()
    self.title.text = getLanguage("Sys_TowerBtn_Record");
    local cfg = G.dataTable["t_stage"][tostring(logicMgr.MapManager.getNextStage())];
    if cfg ~= nil then
        self.curStage.text = cfg.f_ChapterId .. "-" .. cfg.f_StageNum;
    else
        self.curStage.text = "";
    end
    self.dynamicList:RemoveChildrenToPool();
end

function _M:addEvent()
    self.btn_back.onClick:Add(self.onClickBack);
    self.bgClose.onClick:Add(self.onClickBack);
    self:RegisterEvent(Config.EventType.Fresh_DungeonRecord,self.refreshWithData);
end

function _M:removeEvent()
    self.btn_back.onClick:Remove(self.onClickBack);
    self.bgClose.onClick:Remove(self.onClickBack);
end

function _M.onClickBack()
    closeUI(PanelNames.UIChapterRecord);
end

function _M:refreshWithData(recordData)
    if recordData == nil then
        _M.nothingController.selectedIndex = 1;
        return;
    end
    local playerDataList = recordData["playerDataList"];
    if playerDataList == nil or #playerDataList == 0 then
        _M.nothingController.selectedIndex = 1;
    else
        _M.nothingController.selectedIndex = 0;
        for i = 1, #playerDataList do
            local itemobj = _M.dynamicList:AddItemFromPool()
            _M.showData(itemobj,playerDataList[i]);
            if #playerDataList[i]["group"] > 1 then
                itemobj:GetChild("btn_skada").onClick:Add(function()
                    BattlePlaybackManager.setOwnDungeonBattleRecord(BattlePlaybackManager.getBattleRecord())
                    showUI(PanelNames.UITeamsRecode, self.fightType, playerDataList[i]["group"], function()
                        if self.onPlayBegin then
                            self.onPlayBegin();
                        end
                        FightManager.SetRePlay(true);
                        FightManager.SetDungeonWinRePlay(false);
                    end, self.onPlayFinished, self.replay_type, self.foramtion_type);
                end)
            else
                itemobj:GetChild("btn_skada").onClick:Add(function()
                    BattlePlaybackManager.setOwnDungeonBattleRecord(BattlePlaybackManager.getBattleRecord())
                    showUI(PanelNames.UILevelRecode, self.fightType, playerDataList[i]["group"][1]["record"],function()
                        if self.onPlayBegin then
                            self.onPlayBegin();
                        end
                        FightManager.SetRePlay(true);
                        FightManager.SetDungeonWinRePlay(false);
                    end, self.onPlayFinished, self.replay_type, self.foramtion_type);
                end)
            end
        end
    end
end

function _M.showData(obj,data)
    playerName = obj:GetChild("PlayerName")
    power = obj:GetChild("power")
    time = obj:GetChild("time")
    heroList = obj:GetChild("heroList");

    btn_icon = obj:GetChild("icon")
    img_head = obj:GetChild("icon"):GetChild("head"):GetChild("HeadIcon")
    img_frame = obj:GetChild("icon"):GetChild("frame")
    if data ~= nil then
        playerName.text = data["playerName"];
        power.text = getNumString(data["power"]);
        local serverTime = os.date("%Y.%m.%d",tonumber(data["time"])/1000);
        time.text = string.format(getLanguage("StrategyTips"), serverTime);
        local path = logicMgr.SkinManager.GetHeroIconBySkinId1(data.icon)--dataTable.getDataTableByStringId("t_hero",data.icon,"f_HeroIcon")
        HelperFunc.SetIcon(img_head,path,EnumConst.ItemShowType.ICON)
        img_frame.url = "ui://ICONAtlas/" .. data.headFrame
        local heroDataList = {};
        heroDataList = data["heroList"];

        heroList:RemoveChildrenToPool();
        local ctrl = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
        for i = 1, #heroDataList do
            local headObj = heroList:AddItemFromPool()
            local heroconfig = G.dataTable["t_hero"][tostring(heroDataList[i].partnerModelId)];
            local data = {}
            logicMgr.HeroManager.copyHeroConfig(heroconfig, data);
            data.level = heroDataList[i].level;
            data.star = heroDataList[i].star;
            data.quality = heroDataList[i].quality;
            if ctrl ~= nil then
                ctrl.initData(headObj,data);
            end
        end
    end
end

-- 显示
function _M:OnEnable()

end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    _M.hasInit = false
    _M.cancelJumpOut = false;
end

return _M;